package com.sin.j2me.framework;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;

public class NewDebug {
	private static Form form = null;
	
	public static int LEVEL_ALL = 0;
	public static int LEVEL_ERROR = 10;
	public static int LEVEL_WARNING = 8;
	public static int LEVEL_INFORMARION = 6;
	public static int LEVEL_LOG = 4;
	
	private static int level = LEVEL_ALL; 
	
	private static boolean showOnLog = true;
	private static boolean showOnScreen = true;
	private static boolean autoClearLog = false;
	
	private static Command cmdback = null;
	private static Command cmdclear = null;
	
	
	private static int recid = 0;
	
	public static int getLevel() {
		return level;
	}
	public static void setLevel(int level) {
		NewDebug.level = level;
	}
	public static boolean isAutoClearLog() {
		return autoClearLog;
	}
	public static void setAutoClearLog(boolean autoClearLog) {
		NewDebug.autoClearLog = autoClearLog;
	}
	public static boolean isShowOn() {
		return showOnLog;
	}
	public static void setShowOnLog(boolean showOnLog) {
		NewDebug.showOnLog = showOnLog;
	}
	public static boolean isShowOnScreen() {
		return showOnScreen;
	}
	public static void setShowOnScreen(boolean showOnScreen) {
		NewDebug.showOnScreen = showOnScreen;
	}
	public static void e(String err){
		error(err);
	}
	public static void error(String err){
		if(level<=LEVEL_ERROR)
			record("error", err);
	}
	public static void w(String warn){
		warning(warn);
	}
	public static void warning(String warn){
		if(level<=LEVEL_WARNING)
			record("warning", warn);
	}
	public static void i(String info){
		information(info);
	}
	public static void information(String info){
		if(level<=LEVEL_INFORMARION)
			record("information", info);
	}
	public static void l(String log){
		log(log);
	}
	public static void log(String log){
		if(level<=LEVEL_LOG)
			record("log", log);
	}
	public static void record(String tag, String rec){
		tag = tag + "(" +recid+ ")";
		++recid;
		if(showOnLog){
			System.out.println(tag+":");
			System.out.println(rec);
		}
		if(showOnScreen){
			if(form == null){
				form = new Form("CatLog");
				cmdback = new Command("BACK", Command.BACK, 0);
				cmdclear = new Command("CLEAR", Command.STOP, 0);
				form.addCommand(cmdback);
				form.addCommand(cmdclear);
				form.setCommandListener(listener);
			}
			if(autoClearLog){
				form.deleteAll();
			}
			form.setTitle("CatLog:" + recid);
			form.append(new StringItem(tag, rec));
			SinFramework.startDisplay(form);
		}
	}
	
	private static CommandListener listener = new CommandListener() {
		public void commandAction(Command cmd, Displayable arg1) {
			if(cmd == cmdback){
				SinFramework.finishDisplay(form);
			}
			else if(cmd == cmdclear){
				form.deleteAll();
			}
		}
	};
}
